﻿Imports System.Text
Imports Kairyu.Extension

''' <summary>
''' 作業完了時イベント情報
''' </summary>
''' <remarks></remarks>
Public Class FinishEventArgs
    Inherits System.EventArgs

    Public Sub New(ByVal totalmsec As Long, rows As Integer)
        Me.New(totalmsec, rows, New Dictionary(Of String, Long))
    End Sub

    Public Sub New(ByVal totalmsec As Long, rows As Integer, details As Dictionary(Of String, Long))
        Me.TotalTime = totalmsec
        Me.Rows = rows
        Me.Details = details
    End Sub

    Public Property TotalTime As Long

    Public Property Rows As Long

    Public Property Details As Dictionary(Of String, Long)

    Public Function GetDetails() As String
        If Me.Details.Any = False Then Return String.Empty

        Dim s As New StringBuilder
        Dim add As Action(Of String) =
            Sub(val)
                If String.IsNullOrEmpty(val) Then Return
                s.Appenddelimiter(", ")
                s.Append(val)
            End Sub

        For Each item In Details
            add.Invoke(String.Format("{0}={1}ms", item.Key, item.Value.ToString("n0")))
        Next

        Return String.Format("({0})", s.ToString)
    End Function

    Public Overrides Function ToString() As String
        Dim s As New StringBuilder
        Dim add As Action(Of String) =
            Sub(val)
                If String.IsNullOrEmpty(val) Then Return
                s.AppendDelimiter(Space(1))
                s.Append(val)
            End Sub

        'add.Invoke(Me.WorkName)
        If Me.Rows = 1 Then
            add.Invoke(String.Format("{0}row", Me.Rows.ToString("n0")))
        Else
            add.Invoke(String.Format("{0}rows", Me.Rows.ToString("n0")))
        End If

        add.Invoke(String.Format("{0}ms", Me.TotalTime.ToString("n0")))
        add.Invoke(Me.GetDetails)

        Return s.ToString
    End Function

End Class
